<!DOCTYPE html>
<html>
  <head>
    <title>Listing 15.6</title>
    <meta charset="utf-8">
    <script type="text/javascript" src="../scripts/assert.js"></script>
    <link href="../styles/assert.css" rel="stylesheet" type="text/css">
  </head>
  <body>
    <script type="text/javascript">

      var selector = "div.class > span:not(:first-child) a[href]"
      var chunker = /((?:\([^\)]+\)|\[[^\]]+\]|[^ ,\(\[]+)+)(\s*,\s*)?/g;
      var parts = [];

      // Reset the position of the chunker regexp (start from beginning)
      chunker.lastIndex = 0;

      // Collect the pieces
      while ((m = chunker.exec(selector)) !== null) {
        parts.push(m[1]);

        // Stop if we've countered a comma
        if (m[2]) {
          extra = RegExp.rightContext;
          break;
        }
      }

      assert(parts.length == 4,
          "Our selector is broken into 4 unique parts.");
      assert(parts[0] === "div.class", "div selector");
      assert(parts[1] === ">", "child selector");
      assert(parts[2] === "span:not(:first-child)", "span selector");
      assert(parts[3] === "a[href]", "a selector");

    </script>
  </body>
</html>
